package IC.TypeTable;

import IC.AST.*;
import IC.SymbolTable.SemanticError;


public class ClassType extends Type {
	
	private ICClass iCClass_AST;
	private String superClassName;
		
	public ClassType(ICClass iCClass_AST){
		super(iCClass_AST.getName());
		this.iCClass_AST = iCClass_AST;
		this.superClassName = iCClass_AST.getSuperClassName();
		
	}
	
	public ICClass getClassNode()
	{
		return this.iCClass_AST;
	}
	
	public String getSuperClassName()
	{
		return this.superClassName;
	}
	
	
	public boolean isSubTypeOf(Type type){
		// type is not a class type	
		if (!(type instanceof ClassType))
			return false;		
		if (type == this)
			return true;			
		// doesn't have super
		if (this.superClassName == null)
			return false;		
		else
			try{ // try super class
				return TypeTable.getClassType(superClassName).isSubTypeOf(type);	
		} catch (SemanticError error)
		{ 
			return false;
		}
	}
	
	
	public String toString()
	{
		String str = "";

		if (this.iCClass_AST.hasSuperClass())
		{
			try 
			{
				str += TypeTable.getClassType(this.superClassName).getTypeNumber();
			} 
			catch (SemanticError error)
			{
				return this.getName();
			} 
			return this.getName() + ", Superclass ID: " + str;
		}
		return this.getName();
	}
}